/**create by liuhua at 2019年6月13日 上午11:33:57**/
package com.star.truffle.core.mybatis.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.star.truffle.core.jackson.StarJson;

public class ListForStringHandler extends BaseTypeHandler<List<String>> {

  private StarJson starJson = new StarJson(new ObjectMapper());

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
    String val = null;
    if(null != parameter) {
      val = starJson.obj2string(parameter);
    }
    ps.setString(i, val);
  }

  @Override
  public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return starJson.str2list(rs.getString(columnName));
  }

  @Override
  public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    return starJson.str2list(rs.getString(columnIndex));
  }

  @Override
  public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    return starJson.str2list(cs.getString(columnIndex));
  }
}
